import java.util.Stack;

public class Sort {

    /*
    * 冒泡排序
    * 时间复杂度：O(N^2)
    *   最好情况：O(N)
    * 空间复杂度：O(1)
    * 稳定性：稳定
    * */
    public static void bubbSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 -i; j++) {
                if(array[j] > array[j + 1]){
                    swap(array,j,j + 1);
                    flg = true;
                }
            }
            if(!flg){
                break;
            }
        }
    }

    private static void swap(int[] array, int j, int i) {
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }


    /*
    * 快排
    * 时间复杂度：
    *   如果是 1 2 3 4 5 或 5 4 3 2 1 这种单分支的树
    *   此时的效率最低：O(N^2)
    *   但是我们一般很少说 O(N^2) 的时间复杂度
    *   一般都是O(N*logN)
    * 空间复杂度：
    *   最坏的情况：单分支的树：O(N)
    *   最好的情况：O(logN)
    * 稳定性：不稳定
    * */
    public static void quickSort(int[] array){
        quick(array,0,array.length - 1);
    }

    private static void quick(int[] array, int left, int right) {
        if(left >= right){
            return ;
        }
        if(right - left + 1 == 7){
            //使用直接插入排序
            insertSort2(array,left,right);
            return ;
        }
        int midIndex = findMidNum(array,left,right);
        swap(array,left,midIndex);
        int par = partition(array,left,right);
        //递归左树
        quick(array,left,par - 1);
        //递归右树
        quick(array,par + 1,right);
    }

    private static void insertSort2(int[] array, int start, int end) {
        for (int i = start + 1; i <= end; i++) {
            int pivot = array[i];
            int j = i - 1;
            for (; j >= start ; j--) {
                if(array[j] > pivot){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = pivot;
        }
    }

    private static int findMidNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]){
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid] < array[right]){
                return right;
            } else if (array[mid] > array[left]) {
                return left;
            }else{
                return mid;
            }
        }
    }

    private static int partition(int[] array, int start, int end) {
        int i = start;
        int pivot = array[start];
        while(start < end){
            while(start < end && array[end] >= pivot){
                end--;
            }
            while(start < end && array[start] <= pivot){
                start++;
            }
            swap(array,start,end);
        }
        swap(array,start,i);
        return start;
    }

    /*
    * 挖坑法
    * */

    private static int partition2(int[] array,int start,int end){
        int pivot = array[start];
        while(start < end){
            while(start < end && array[end] >= pivot){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= pivot){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = pivot;
        return start;
    }

    /*
    * 前后指针法
    * */

    private static int partition3(int[] array,int left,int right){
        int cur = left + 1;
        int prev = left;
        while(cur <= right){
            if(array[cur] < array[left] && array[++prev] != array[cur]){
                swap(array,cur,prev);
            }else{
                cur++;
            }
        }
        swap(array,left,prev);
        return prev;
    }

    /*
    * 快排非递归
    * */

    public static void quickSort2(int[] array){
        int left = 0;
        int right = array.length - 1;
        int par = partition(array,left,right);
        Stack<Integer> stack = new Stack<>();
        if(par > left + 1){
            stack.push(left);
            stack.push(par  - 1);
        }
        if(par < right - 1){
            stack.push(par + 1);
            stack.push(right);
        }
        while(!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);
            if(par > left + 1){
                stack.push(left);
                stack.push(par  - 1);
            }
            if(par < right - 1){
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

    public static void quick7(int[] array){
        int left = 0;
        int right = array.length - 1;
        quick7_1(array,left,right);
    }

    public static void quick7_1(int[] array,int left,int right){
        if(left >= right){
            return ;
        }
        int par = partition7(array,left,right);
        quick7_1(array,left,par - 1);
        quick7_1(array,par + 1,right);
    }
    private static int partition7(int[] array,int start,int end){
        int pivot = array[start];
        while(start < end){
            while(start < end && array[end] > pivot){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] < pivot){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = pivot;
        return start;
    }
}
